/* 
 * File:   Glyphs.cpp
 * Author: cjjonker
 * 
 * Created on 20 december 2012, 16:06
 */

#include "Glyphs.h"
#include "GL/glut.h"
#include "Utilities.h"
using namespace std;

Glyphs::Glyphs()
{
    d_has_multiple_appearances = true;
    d_multiple_appearances = {"Hedgehogs", "Triangles", "Cones"};
    d_uses_vector_dataset = true;
    d_name = "Vector field visualization";
    d_legend.setName("Vector");
    
}

void Glyphs::draw(float winW, float winH, int t, int slices, int sliceDT)
{   
    int i, j, idx; 
    const int DIM = d_simulation->getDim();

    fftw_real  wn = (fftw_real)winW / (fftw_real)(DIM + 1);   // Grid cell width
    fftw_real  hn = (fftw_real)winH / (fftw_real)(DIM + 1);  // Grid cell heigh

    double gx,gy;
    Vector2f v;
        
    float dx = float(DIM - 1) / (d_xGridSize - 1);
    float dy = float(DIM - 1) / (d_yGridSize - 1);

    for (i = 0; i < d_xGridSize; i++)
    {
        for (j = 0; j < d_yGridSize; j++)
        {
            idx = (j * DIM) + i;
            gx = wn + i * wn * dx;
            gy = hn + j * hn * dy;
            
            v = d_vector_dataset->getData(i * dx, j * dy, t);
            
            if (d_normalize)
            {
                v.normalize();
                v *= 0.01;
            }
            
            v *= d_scale;

            setGLColor(i*dx, j*dy, t);
            
            drawGlyph(gx,gy,v,wn,hn);
        }

    }
    
    
   
    
    /*
    else if (d_glyphtype == Triangle)
    {
        
    }*/
}

void Glyphs::drawGlyph(double x, double y, Vector2f dat, double wn, double hn)
{
    if (d_appearance==Hedgehog)
    {
        drawHedgehog(x,y,dat,wn,hn);
    }
    else if (d_appearance==Triangle)
    {
        drawTriangle(x,y,dat,wn,hn);
    }
    else if (d_appearance==Cone)
    {
        drawCone(x,y,dat,wn,hn);
    }
    else
    {
        d_appearance==Hedgehog;
        drawHedgehog(x,y,dat,wn,hn);
    }
}

void Glyphs::drawHedgehog(double x, double y, Vector2f dat, double wn, double hn)
{
    glBegin(GL_LINES);
        glVertex2f(x,y);
        glVertex2f(x+wn*50*d_scale*dat.x, y+hn*50*d_scale*dat.y);
    glEnd();
}

void Glyphs::drawTriangle(double x, double y, Vector2f dat, double wn, double hn)
{
    float angle = rad2deg(atan2(dat.x, dat.y));
    float size = dat.length()*d_scale*50;

    glPushMatrix();
    glTranslatef(x,y,0.0);
    glScalef(wn,hn,0.0);
    glRotatef(angle, 0.0, 0.0, 1.0);

    glBegin(GL_TRIANGLES);
        glVertex2f(-0.2, 0.0);
        glVertex2f(0.2, 0.0);
        glVertex2f(0.0, size);
    glEnd();
    glPopMatrix();
}

void Glyphs::drawCone(double x, double y, Vector2f dat, double wn, double hn)
{
    float angle = rad2deg(atan2(dat.x, dat.y));
    float size = dat.length()*d_scale;

    glPushMatrix();   

    glTranslatef(x,y,0.0); //draw from this point    
    
    glScalef(wn,hn,0.0);
    glRotatef(110.0, 1.0, 0.0, 0.0); 
    glRotatef(angle+180.0, 0.0, 1.0, 0.0);    

    glutSolidCone(0.2, 25.0*size, 5, 5);

    glPopMatrix();
}
